package com.mzy.linear_struct.subject;

import java.util.SortedSet;

/**
 * @Author: codermzy
 * @Date: 2024/03/30/19:38
 * @Description:
 */
public class BinarySearch {


    public static void main(String[] args) {
        int[] arr = {-1, 0, 3, 5, 9, 12};

        int index = binarySearch(arr, 13);
        System.out.println(index);
    }

    private static int binarySearch(int[] arr, int value) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] > value) {
                right = mid - 1;
            } else if (arr[mid] < value) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
